import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  HttpStatus,
  Delete,
} from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './entities/user.entity';
import { CreateUserDto } from './dto/create-user.dto';
import { FindUserDto } from './dto/find-user.dto';
// import { UserById } from './pipes/userById.pipe';
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  // 创建用户
  @Post('users')
  async create(@Body() body: CreateUserDto): Promise<User> {
    return await this.userService.create(body);
  }

  // 查询所有的用户(分页)
  @Get('users')
  findAndCount(@Query() query: FindUserDto) {
    return this.userService.findAndCount(query);
  }

  // 通过 id 获取单个用户信息
  // @Get('one/:id')
  // async findOne(@Param('id') id: User) {
  //   return this.userService.findOne(id);
  // }
  @Get('id')
  async findOne(@Query() id: User) {
    return await this.userService.findOne(id.id);
  }

  // 用户名的模糊查询
  @Get('like/:username')
  async likeByUserName(
    @Param('username') username: User,
    @Query() query: FindUserDto,
  ) {
    return this.userService.likeByUserName(username, query);
  }

  // 修改用户密码
  @Post('users/:id/password')
  async updatePassword(@Param('id') id: User, @Body() user: User) {
    return this.userService.updatePassword(id, user);
  }

  //  删除指定用户
  @Delete('delete/:id')
  async deleteById(@Param('id') id: User) {
    await this.userService.deleteById(id);
    return {
      statusCode: HttpStatus.OK,
      message: '删除用户成功！',
    };
  }
}
